package net;

import org.apache.http.Header;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.poi.ss.usermodel.Workbook;
import util.ExcelUtils;
import util.SightRegex;
import util.Spider;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.util.*;

/**
 * @fileName: CtripGrab
 * @author: Han
 * @date: 2018/5/24 10:47
 * @dscription:
 */
public class CtripGrab {

    public static void main(String... args) {
        int count = 396;
        List<Map<String, String>> discussMapList = new ArrayList<>();
        CtripGrab ctripGrab = new CtripGrab();
        for (int i = 1; i <= count; i++) {
            try{
                List<Map<String, String>> tempList = ctripGrab.getDiscuss(i);
                discussMapList.addAll(tempList);
                System.out.println("第" + i + "页检索完成,共" + tempList.size() + "条数据");
                //10秒一次
                Thread.sleep(1000 * 10);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        //key:sheet名,value:sheet里的内容
        Map<String, List<Map<String, String>>> sheetMap = new HashMap<>(16);
        sheetMap.put("携程", discussMapList);
        ExcelUtils.writeToLocal("F:/南京海底世界-携程.xls",sheetMap);
    }

    private List<Map<String, String>> getDiscuss(int pageNum) throws Exception {

        Header[] headers = {
                new BasicHeader("Host", "you.ctrip.com"),
                new BasicHeader("Connection", "keep-alive"),
                new BasicHeader("Accept", "*/*"),
                new BasicHeader("Origin", "http://you.ctrip.com"),
                new BasicHeader("X-Requested-With", "XMLHttpRequest"),
                new BasicHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36"),
                new BasicHeader("Content-Type", "application/x-www-form-urlencoded"),
                new BasicHeader("Referer", "http://you.ctrip.com/sight/nanjing9/1654.html"),
                new BasicHeader("Accept-Encoding", "gzip, deflate"),
                new BasicHeader("Accept-Language", "zh-CN,zh;q=0.9"),
        };
        NameValuePair[] nameValuePairs = {
                new BasicNameValuePair("poiID", "75696"),
                new BasicNameValuePair("districtId", "9"),
                new BasicNameValuePair("districtEName", "Nanjing"),
                new BasicNameValuePair("pagenow", pageNum + ""),
                new BasicNameValuePair("order", "3.0"),
                new BasicNameValuePair("star", "0.0"),
                new BasicNameValuePair("tourist", "0.0"),
                new BasicNameValuePair("resourceId", "1654"),
                new BasicNameValuePair("resourcetype", "2"),
        };
        List<NameValuePair> nameValuePairList = Arrays.asList(nameValuePairs);
        String cTripUrlStr = "http://you.ctrip.com/destinationsite/TTDSecond/SharedView/AsynCommentView";
        Spider spider = new Spider();
        String pageStr = spider.getPageByPost(cTripUrlStr, headers, nameValuePairList);
        SightRegex sightRegex = new SightRegex();
        return sightRegex.cTrip(pageStr,pageNum);
    }
}
